﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Iguana.Core
{
    /// <summary>
    /// Provides tools and features for Iguana Markup Language generation.
    /// </summary>
    public class IMLGenerator
    {

        private IMLV m_IML_Version;
        private List<IEmittable> m_EmiitableObjects;

        public IMLV IMLVersion
        {
            get { return m_IML_Version; }
            set { m_IML_Version = value; }
        }

        public IMLGenerator(IMLV IML_VERSION)
        {
            m_IML_Version = IML_VERSION;
            m_EmiitableObjects = new List<IEmittable>();
        }

        /// <summary>
        /// Returns a new Iguana Markup Language IMLGenerator class with the most current, stable IML version.
        /// This is the recommended way to create an instance of IMLGenerator.
        /// </summary>
        /// <returns></returns>
        public static IMLGenerator CreateGenerator()
        {
            return new IMLGenerator(IMLV.IMLV1);
        }

        /// <summary>
        /// Adds an emittable instruction to the generators stack.
        /// </summary>
        /// <param name="obj"></param>
        public void IMLAdd(IEmittable obj)
        {
            if (obj != null)
                m_EmiitableObjects.Add(obj);
        }

        /// <summary>
        /// Outputs standard HTML, CSS, and JavaScript markup from IML.
        /// </summary>
        public IEmissionResult Emit()
        {
            // String to hold our emission result.
            string m_Result = String.Empty;

            // Iterate all emittable Objects and call their Out() function.
            foreach (var e in m_EmiitableObjects)
            {
                m_Result += e.Out();
            }

            return new m_EmissionResultBase(m_Result);
        }

        /// <summary>
        /// Provides a way to return an interface from the Emit() method.
        /// </summary>
        private class m_EmissionResultBase : IEmissionResult
        {

            private object m_Content;

            public m_EmissionResultBase(object result)
            {
                m_Content = result;
            }

            public object Content
            {
                get { return m_Content; }
            }
        }

    }
}
